package org.test.client.ui.util;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;

import org.test.client.ui.skinui.BEUIInstall;
import org.test.client.ui.util.run.FadeWindowRun;

public class UIUtil {
	
	public static AlphaComposite getSrcOverAC( float alpha){
		AlphaComposite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
		return newComposite;
	}
	
	public static Paint getPaint(Color c1,Color c2,int x1,int y1,int x2,int y2){
		GradientPaint paint1 = new GradientPaint(x1, y1, c1, x2, y2, c2);
		return paint1;
	}
	
	public static Stroke getStroke(int width){
		BasicStroke bs1 = new BasicStroke(width, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
		return bs1;
	}
	
	public static void setWindowOpacity(Window w,float a){
		com.sun.awt.AWTUtilities.setWindowOpacity(w, a);
	}
	
	public static void setWindowOpaque(Window w, boolean a){
		com.sun.awt.AWTUtilities.setWindowOpaque(w, a);
	}
	
	public static int getStringWidth(String txt,FontMetrics fm){
		return fm.stringWidth(txt);
	}
	
	public static int getTxtHeight(FontMetrics fm){
		int ascent = fm.getAscent();
		int descent = fm.getDescent();
		return (ascent+descent);
	}
	
	public static int getTxty(Graphics2D g2d){
		FontMetrics fm = g2d.getFontMetrics();
		int ascent = fm.getAscent();
		int descent = fm.getDescent();
		return (ascent-descent)/2;
	}
	
	public static int getTxtWidth(Graphics2D g2d,String txt){
		if(txt == null){
			return 0;
		}
		FontMetrics fm = g2d.getFontMetrics();
		return fm.stringWidth(txt);
	}
	
	public static int getTxtHeight(Graphics2D g2d){
		FontMetrics fm = g2d.getFontMetrics();
		return fm.getAscent()+fm.getDescent();
	}
	
	public static void fadeOutWindow(Window w){
		FadeWindowRun run = new FadeWindowRun(w);
		new Thread(run).start();
	}
	
	public static void fadeOutWindow(Window w,int frames){
		FadeWindowRun run = new FadeWindowRun(w);
		run.setFrames(frames);
		new Thread(run).start();
	}
	
	public static void fadeOutWindowNoDispose(Window w,int frames){
		FadeWindowRun run = new FadeWindowRun(w);
		run.setDispose(false);
		run.setFrames(frames);
		new Thread(run).start();
	}
	
	public static void fadeInWindow(Window w){
		FadeWindowRun run = new FadeWindowRun(w,false,false);
		new Thread(run).start();
	}
	
	public static void fadeInWindow(Window w, int frames){
		FadeWindowRun run = new FadeWindowRun(w,false,false);
		run.setFrames(frames);
		new Thread(run).start();
	}

	public static void centerWindow( Window w){
		w.setLocationRelativeTo(null);
	}
	
	public static Dimension getScreenSizeWithinInsets(JFrame f){
		Toolkit t = Toolkit.getDefaultToolkit();
		Dimension ss = t.getScreenSize();
		GraphicsConfiguration gcf = null;
		if(f != null){
			gcf = f.getGraphicsConfiguration();
		}else{
			gcf = Icons.deCfg;
		}
		Insets screenInsets = t.getScreenInsets(gcf);
		ss.width = ss.width - (screenInsets.left + screenInsets.right)  ;
		ss.height = ss.height - (screenInsets.top + screenInsets.bottom ) ;
		return ss;
	}
	
	public static void initSkin(){
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch ( Exception e) {
			e.printStackTrace();
		} 
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		
		BEUIInstall.installUI();
	}
	

	
	////////////////////////////////////////以下代码移植beautyEye//////////////////////////////////////////
	/**
	 * 绘制虚线框（本方法适用于对4个边的可选绘制情况下，可能会有4舍5入的小误差
	 * ，除了要可选绘制4个边外，一般不推荐使用）.<br>.
	 *
	 * @param g the g
	 * @param x the x
	 * @param y the y
	 * @param width the width
	 * @param height the height
	 */
    public static void drawDashedRect(Graphics g,int x,int y,int width,int height) 
    {
    	drawDashedRect(g,x,y,width,height,6,6,2,2);
    }

	/**
	 * 绘制虚线框（本方法适用于对4个边的可选绘制情况下，可能会有4舍5入的小误差
	 * ，除了要可选绘制4个边外，一般不推荐使用）.
	 *
	 * @param g the g
	 * @param x the x
	 * @param y the y
	 * @param width the width
	 * @param height the height
	 * @param arcWidth the arc width
	 * @param arcHeight the arc height
	 * @param separator_solid 虚线段实线长度
	 * @param separator_space 虚线段空白长度
	 * add by js,2009-08-30
	 */
    public static void drawDashedRect(Graphics g,int x,int y,int width,int height
    		,int arcWidth, int arcHeight, int separator_solid, int separator_space) 
    {
//    	drawDashedRect(g,x,y,width,height,step,true,true,true,true);
    	setAntiAliasing((Graphics2D)g, true);
    	
    	//虚线样式
		Stroke oldStroke = ((Graphics2D)g).getStroke();
		Stroke sroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_BEVEL, 0, new float[]{separator_solid, separator_space}, 0);//实线，空白
		((Graphics2D)g).setStroke(sroke);
		
		g.drawRoundRect(x, y
				, width-1, height-1 //* 一个很诡异的问题：使用BasicStroke实现虚线绘制后，似乎绘制的距形
									//* 要比普通方法绘制实线距形往下偏移一个坐标，此处-1是为了修正这个问题，这难道是java的bug？
									//* 难怪当初打印工具开发时也遇到了莫名其妙偏移一个像素的现象，具体有待进一步研究！
				, arcWidth, arcHeight);
		
		((Graphics2D)g).setStroke(oldStroke);
		setAntiAliasing((Graphics2D)g, false);
    }
    
	/**
	 * 图形绘制反走样设置.	:抗锯齿
	 *
	 * @param g2 the g2
	 * @param antiAliasing 是否反走样
	 */
	public static void setAntiAliasing(Graphics2D g2 ,boolean antiAliasing)
	{
		if(antiAliasing)
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING
					, RenderingHints.VALUE_ANTIALIAS_ON);
		else
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING
					, RenderingHints.VALUE_ANTIALIAS_OFF);
	}
	
	
	public static void setAntiAliasing(Graphics g ,boolean antiAliasing)
	{
		setAntiAliasing((Graphics2D)g,antiAliasing);
	}
	
	/**
	 * 填充3角形区域.
	 *
	 * @param g the g
	 * @param x1 3个点之一的x坐标
	 * @param y1 3个点之一的y坐标
	 * @param x2 3个点之一的x坐标
	 * @param y2 3个点之一的y坐标
	 * @param x3 3个点之一的x坐标
	 * @param y3 3个点之一的y坐标
	 * @param c the c
	 */
	public static void fillTriangle(Graphics g
			,int x1,int y1,int x2,int y2
			,int x3,int y3,Color c)
	{
		int[] x = new int[3],y = new int[3];
		// A simple triangle.
		x[0]=x1; x[1]=x2; x[2]=x3;
		y[0]=y1; y[1]=y2; y[2]=y3;
		int n = 3;

		Polygon p = new Polygon(x, y, n);  // This polygon represents a triangle with the above
		                                   //   vertices.
		g.setColor(c);
		g.fillPolygon(p);     // Fills the triangle above.
	}
	
}
